package deliciouse.biz.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import deliciouse.biz.NewsBiz;
import deliciouse.dao.MessagesDao;
import deliciouse.dao.NewsDao;
import deliciouse.entity.News;
import deliciouse.util.MybatisUtil;

public class NewsBizImpl implements NewsBiz{

	@Override
	public List<News> getall() {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
		NewsDao dao = sess.getMapper(NewsDao.class);
			List<News> newslist = dao.getall();
			return newslist;
		} finally {
			sess.close();
		}
	}

	@Override
	public List<News> fetchByDate(int pageNum, int pageSize) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
		NewsDao dao = sess.getMapper(NewsDao.class);
			List<News> newslist = dao.fetchByDate((pageNum-1)*pageSize, pageSize);
			return newslist;
		} finally {
			sess.close();
		}
	}

	@Override
	public int MessageMaxRows() {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			NewsDao dao = sess.getMapper(NewsDao.class);
			int rows =dao.MessageMaxRows();
			return rows;
		} finally {
			sess.close();
		}
	}

	@Override
	public News fetchById(int id) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			NewsDao dao = sess.getMapper(NewsDao.class);
			News news =dao.fetchById(id);
			return news;
		} finally {
			sess.close();
		}
	}

	@Override
	public List<News> fetchByCondition(String title) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			NewsDao dao = sess.getMapper(NewsDao.class);
			List<News> newslist =dao.fetchByCondition(title);
			return newslist;
		} finally {
			sess.close();
		}
	}

	@Override
	public boolean updateNews(News news) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			NewsDao dao = sess.getMapper(NewsDao.class);
			dao.updateNews(news);
			sess.commit();
			return true;
		} finally {
			sess.close();
		}
	}

	@Override
	public boolean addnews(News news) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			NewsDao dao = sess.getMapper(NewsDao.class);
			dao.addnews(news);
			sess.commit();
			return true;
		} finally {
			sess.close();
		}
	}

	@Override
	public boolean deletenews(int id) {
		SqlSession sess = MybatisUtil.OpenSession();
		try {
			NewsDao dao = sess.getMapper(NewsDao.class);
			dao.deletenews(id);
			sess.commit();
			return true;
		} finally {
			sess.close();
		}
	}

}
